package com.genius.unite_platform.goods_order.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.other.entity.RoleMenuJson;
import com.genius.adminmanager.other.entity.TreeJson;
import com.genius.workflow.element.SQLBean;
import com.genius.workflow.util.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * 树形表格操作类
 * @author woy
 *
 */
public class TreeUtils1 {
	/**
	 * 根据表名，关键字，父id删除所有的数据
	 * @param table
	 * @param idKey
	 * @param idValue
	 * @param pId
	 * @return
	 * @throws SQLException 
	 * @throws DBClosedException
	 * @throws ConnectionException
	 */
	public static boolean delByIdTree(String table,String idKey,String idValue,String pId) throws ConnectionException, DBClosedException, SQLException{
		boolean isSuccess = false;
		ArrayList<String[]> parms = new ArrayList<>();
		Set<String> idKeys = queryIdKey(table, idKey, idValue, pId);
		for(String id:idKeys){
			parms.add(new String[]{id});
		}
		String del = "delete from "+table+" where "+idKey+"=?";
		SQLBean bean = new SQLBean();
		bean.setSql(del);
		bean.setParms(parms);
		DBTools.execute(bean);
		return isSuccess;
	}
	private static Set<String> queryIdKey(String table,String idKey,String idValue,String pId) throws ConnectionException, DBClosedException, SQLException{
		Set<String> idKeys = new HashSet<>();
		idKeys.add(idValue);
		
		String idKeyQuery = "select "+idKey+" from "+table+" where "+pId+"=?";
		JSONArray result = DBUtils.selectArr(idKeyQuery, new String[]{idValue});
		for(int index=0;index<result.size();index++){
			JSONObject ids = result.getJSONObject(index);
			String ids_par = ids.getString(idKey);
			idKeys.add(ids_par);
			idKeys.addAll(queryIdKey(table, idKey, ids_par, pId));
		}
		return idKeys;
	}
	/**
	 * 根据resource 、where、orderby 一起查询树形结构
	 * 
	 * @param treeIdField 树形表的关键字
	 * @param field  树形表的字段
	 * @param table  树形表的表名称
	 * @param state  树形表的级别
	 * @param where  查询条件
	 * @param resource  转换的类资源
	 * @param orderBy  根据某一字段排序
	 * @return
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static JSONArray getTree(String treeIdField,String field,String table,String state,JSONObject resource,String where,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		return getTree(treeIdField, field, table, state, where, resource,null,orderBy);
	}
	public static JSONArray getOnceTree(String treeIdField,String idValue,String field,String table,JSONObject resource,String where,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		return getOnceTreeById(treeIdField, idValue, field, table, resource,null,where, orderBy);
	}
	/**
	 * 根据where和orderby 条件跟排序综合查询树形结构
	 * 
	 * @param treeIdField 树形表的关键字
	 * @param field 树形表的字段
	 * @param table 树形表的表名称
	 * @param state 树形表的级别
	 * @param where 查询条件
	 * @param orderBy 根据某一字段排序
	 * @return
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static JSONArray getTree(String treeIdField,String field,String table,String state,String where,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		return getTree(treeIdField, field, table, state, where, null,null,orderBy);
	}
	public static JSONArray getOnceTree(String treeIdField,String idValue,String field,String table,String where,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		return getOnceTreeById(treeIdField, idValue, field, table, null,null,where,orderBy);
	}
	/**
	 * 根据where条件查询树形结构树
	 * 
	 * @param treeIdField 树形表的关键字
	 * @param field 树形表的字段
	 * @param table 树形表的表名称
	 * @param state 树形表的级别
	 * @param where 查询条件
	 * @return
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static JSONArray getTree(String treeIdField,String field,String table,String state,String where) throws ConnectionException, DBClosedException, SQLException{
		return getTree(treeIdField, field, table, state, where, null,null,null);
	}
	public static JSONArray getOnceTree(String treeIdField,String idValue,String field,String table,String where) throws ConnectionException, DBClosedException, SQLException{
		return getOnceTreeById(treeIdField, idValue, field, table, null,null,where,null);
	}
	/**
	 * 普通查询树形结构，没用任何条件
	 * @param treeIdField 树形表的关键字
	 * @param field 树形表的字段
	 * @param table 树形表的表名称
	 * @param state 树形表的级别
	 * @return
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static JSONArray getTree(String treeIdField,String field,String table,String state) throws ConnectionException, DBClosedException, SQLException{
		return getTree(treeIdField, field, table, state, null, null,null,null);
	}
	public static JSONArray getOnceTree(String treeIdField,String idValue,String field,String table) throws ConnectionException, DBClosedException, SQLException{
		return getOnceTreeById(treeIdField, idValue, field, table, null,null,null,null);
	}
	/**
	 * 
	 * @param treeIdField 树形表的关键字
	 * @param field 树形表的字段
	 * @param table 树形表的表名称
	 * @param state 树形表的级别
	 * @param where 查询条件
	 * @param resource  转换的类资源
	 * @param orderBy 根据某一字段排序
	 * @param treeIds  说明要判断授权状态
	 * @return
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static JSONArray getTreeAndPermission(String treeIdField,String field,String table,String state,JSONObject resource,String [] treeIds,String where,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		return getTree(treeIdField, field, table, state, where, resource,treeIds,orderBy);
	}
	public static JSONArray getOnceTreeAndPermission(String treeIdField,String idValue,String field,String table,JSONObject resource,String [] treeIds,String where,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		return getOnceTreeById(treeIdField, idValue, field, table, resource,treeIds,where,orderBy);
	}
	/**
	 * 属于一次性全部查完
	 * 根据显示的字段及表名获取改表的树形列表结构  
	 * @param treeIdField  改树形列表的主键id字段
	 * @param field
	 * @param table
	 * @param state 树形表数据必须得有个层级字段掌控，才能从上往下正确获取
	 * @param where  如果树形列表有条件跟着，就加上条件，如果没有就为空
	 * @param resource  树形列表转换的的资源
	 * @param treeIds  如果不为空，说明要判断授权状态
	 * @return
	 * @throws SQLException 
	 * @throws DBClosedException
	 * @throws ConnectionException
	 */
	private static JSONArray getTree(String treeProwerIdField,String field,String table,String state,String where,JSONObject resource,String [] treeIds,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		if(where == null){
			where = "";
		}
		if(orderBy == null)
			orderBy = "";
		String sql = "select "+field+" from "+table+" where "+state+"=1 "+where+" "+orderBy;
		JSONArray datas = DBUtils.selectArr(sql);
		
		JSONArray TreeArr = new JSONArray();
		for(int i=0;i<datas.size();i++){
			JSONObject tree = datas.getJSONObject(i);
			String treeId = tree.getString(treeProwerIdField);
			
			addPermission(tree, treeIds, treeId);
			
			if(resource!=null){
				tree = GeniusJsonUtil.convertResource(tree,resource);
			}
			JSONArray subTree = getTreeById(treeProwerIdField,tree.getString(treeProwerIdField),field,table,treeIds,orderBy);
			tree.put("children", subTree);
			TreeArr.add(tree);
		}
		return TreeArr;
	}
	/**
	 * 懒加载
	 * 根据顶层菜单及要找的field字段去找相应的子类菜单
	 * @param menuId
	 * @return
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static JSONArray getOnceTreeById(String treeIdField,String idValue,String field,String table,JSONObject resource,String [] treeIds,String where,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		if(idValue == null){
			idValue = "-1";
		}
		if(where == null){
			where = "";
		}
		if(orderBy == null)
			orderBy = "";
		String selectSub = "select "+field+" from "+table+" where "+TreeJson1.fatherid+"=? "+where+" "+orderBy;
		JSONArray subTreeList = DBUtils.selectArr(selectSub,new String[]{idValue});
		
		JSONArray menuArr = new JSONArray();
		for(int index=0;index<subTreeList.size();index++){
			JSONObject subTree = subTreeList.getJSONObject(index);
			String treeId = subTree.getString(treeIdField);
			
			addPermission(subTree, treeIds, treeId);
			
			if(resource!=null){
				subTree = GeniusJsonUtil.convertResource(subTree,resource);
			}
			String queryHaveChildCount = "select count(1)num from "+table+" where "+TreeJson1.fatherid+"=? ";
			JSONObject haveChildJson = DBUtils.select(queryHaveChildCount,new String[]{subTree.getString(treeIdField)});
			
			if(haveChildJson.getInteger("num")==0)
				subTree.put("haveChild", false);
			else{
				subTree.put("haveChild", true);
			}
			menuArr.add(subTree);
		}
		return menuArr;
	}
	private static void addPermission(JSONObject json,String [] treeIds,String treeId){
		if(treeIds == null)
			return;
		for(String treeIds_:treeIds){
			if(treeIds_.equals(treeId)){
				json.put(RoleMenuJson.permissionStatus, "1");
				break;
			}else{
				json.put(RoleMenuJson.permissionStatus, "0");
			}
				
		}
	}
	/**
	 * 根据顶层菜单及要找的field字段去找相应的子类菜单
	 * @param menuId
	 * @return
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static JSONArray getTreeById(String treeIdField,String idValue,String field,String table,String [] treeIds,String orderBy) throws ConnectionException, DBClosedException, SQLException{
		
		if(orderBy == null){
			orderBy = "";
		}
		JSONArray menuArr = new JSONArray();
		String selectSub = "select "+field+" from "+table+" where "+TreeJson.parentId+"=? "+orderBy;
		
		JSONArray subTreeList = DBUtils.selectArr(selectSub,new String[]{idValue});
		
		for(int index=0;index<subTreeList.size();index++){
			JSONObject subTree = subTreeList.getJSONObject(index);
			String treeId = subTree.getString(treeIdField);
			
			addPermission(subTree, treeIds, treeId);
			
			GeniusJsonUtil.convertResource(subTree,MenuJson.resource);
			JSONArray subTree_3 = getTreeById(treeIdField,subTree.getString(treeIdField),field,table,treeIds,orderBy);
			if(subTree_3.size()!=0)
				subTree.put("children", subTree_3);
			menuArr.add(subTree);
		}
		return menuArr;
	}
	/**
	 * 根据表名，参数字段级参数值新增树
	 * @param field
	 * @param table
	 * @param json
	 * @throws ConnectionException
	 * @throws DBClosedException
	 * @throws SQLException
	 */
	public static void addTree(String field,String table,JSONObject json) throws ConnectionException, DBClosedException, SQLException{
		
//		String insertSql = "insert into "+table+"("+field+")"+DBTools.getInsertSqlValues(field);
		DBTools.insert(field, table, json);
	}
	
	public static JSONObject getChildren(JSONObject datas,String name,String value){
		datas.put("value", datas.getString(value));
		datas.put("name", datas.getString(name));
		
		if(datas.containsKey("children")){
			JSONArray children = new JSONArray();
			for(int index = 0;index<datas.getJSONArray("children").size();index++){
				JSONObject childrens = getChildren(datas.getJSONArray("children").getJSONObject(index),name,value);
				//三级目录 去掉按钮 一级
				if(childrens != null){
					children.add(childrens);
				}
			}
			datas.put("children", children);
		}
		return datas;
	}
}
